<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>
<!DOCTYPE HTML>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>出厂编号管理</title>
    <jsp:include page="/resources/inc/head.jsp" flush="true"/>
    <style>
        .link_a{
            text-decoration:underline;
            color:dodgerblue;
        }
    </style>
</head>
<body>
<div id="main">
    <div id="collapseOne" class="panel-collapse collapse">
        <div class="panel panel-default" id="queryHead">
            <div class="panel-body">
                <form id="queryForm" method="post" class="form-horizontal" onsubmit="return false;">
                    <div class="form-group" >
                        <label class="col-sm-2 search-control control-label" >开始时间：</label>
                        <div class="col-sm-2 search-control">
                            <input type="text" id="begintime" name="begintime"  class="form-control" data-date-format="yyyy-mm-dd" placeholder="开始时间" value="" pattern="yyyy-MM-dd" />
                        </div>
                        <label class="col-sm-2 search-control control-label" >结束时间：</label>
                        <div class="col-sm-2 search-control">
                            <input type="text" id="endtime" class="form-control" name="endtime"  data-date-format="yyyy-mm-dd" placeholder="结束时间" value="" pattern="yyyy-MM-dd" />
                        </div>
                        <label class="col-sm-2 control-label search-control" >年代：</label>
                        <div class="col-sm-2 search-control">
                            <input type="text" class="form-control" id="years" name="years" placeholder="年代号,二位阿拉伯数字"  value=""/>
                        </div>
                        <label class="col-sm-2 control-label search-control" >产品分类代号：</label>
                        <div class="col-sm-2 search-control">
                            <select id="classcode" name="classcode" class="form-control">
                                <option value="">请选择--</option>
                            </select>
                        </div>
                        <label class="col-sm-2 search-control control-label">型号代号：</label>
                        <div class="col-sm-2 search-control">
                            <select id="version" name="version" class="form-control">
                                <option value="">请选择--</option>
                            </select>
                        </div>
                        <label class="col-sm-2 search-control control-label">换代或变型代号：</label>
                        <div class="col-sm-2 search-control">
                            <select id="subversion" name="subversion" class="form-control">
                                <option value="">请选择--</option>
                            </select>
                        </div>
                        <label class="col-sm-2 search-control control-label">同步自有平台：</label>
                        <div class="col-sm-2 search-control">
                            <select id="status" name="status" class="form-control">
                                <option value="">请选择--</option>
                            </select>
                        </div>
                        <label class="col-sm-2 search-control control-label">使用去向：</label>
                        <div class="col-sm-2 search-control">
                            <select id="direction" name="direction" class="form-control">
                                <option value="">请选择--</option>
                            </select>
                        </div>
                        <label class="col-sm-2 search-control control-label">完整出厂编号：</label>
                        <div class="col-sm-2 search-control">
                            <input type="text" class="form-control" id="machineCode" name="machineCode" placeholder="完整出厂编号"  value=""/>
                        </div>
                        <div class="col-sm-2 search-control" >
                            <button type="button" class="btn btn-primary" id="search_btn">查询</button>
                            <button type="reset" class="btn btn-primary" id="reset_btn">重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div id="toolbar">
        <shiro:hasPermission name="umbrella:serialnum:create"><a class="waves-effect waves-button" href="javascript:;" onclick="createAction()"><i class="zmdi zmdi-plus"></i> 批量新增(按规则)</a></shiro:hasPermission>
        <shiro:hasPermission name="umbrella:serialnum:create"><a class="waves-effect waves-button" href="javascript:;" onclick="createCustomAction()"><i class="zmdi zmdi-plus"></i> 批量新增(自定义)</a></shiro:hasPermission>
        <shiro:hasPermission name="umbrella:serialnum:delete"><a class="waves-effect waves-button" href="javascript:;" onclick="deleteAction()"><i class="zmdi zmdi-close"></i> 删除</a></shiro:hasPermission>
        <shiro:hasPermission name="umbrella:serialnum:deliver"><a class="waves-effect waves-button" href="javascript:;" onclick="deliverAction()"><i class="glyphicon glyphicon-edit"></i> 批量出货更新</a></shiro:hasPermission>
        <shiro:hasPermission name="umbrella:serialnum:synownplatform"><a class="waves-effect waves-button" href="javascript:;" onclick="synownplatformAction()"><i class="glyphicon glyphicon-import"></i> 同步到自有平台</a></shiro:hasPermission>
        <a class="waves-effect waves-button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne"><i class="zmdi zmdi-search"></i> 高级搜索</a>
    </div>
    <table id="table"></table>
</div>
<jsp:include page="/resources/inc/footer.jsp" flush="true"/>
<jsp:include page="/resources/inc/export.jsp" flush="true"/>

<script>
    //初始化
    //UM_MACHINE_TYPE
    getSelectOption('UM_MACHINE_TYPE','version');
    getSelectOption('UM_UMBRELLA_CLASSCODE','classcode');
    getSelectOption('UM_UMBRELLA_MSUBVERSION','subversion');
    getSelectOption('UPMS_YES_NO','status');
    getSelectOption('UM_UMBRELLA_MDIRECTION','direction');
    //查询按钮事件
    $('#search_btn').click(function() {
        $('#table').bootstrapTable('refresh', {
            //得到查询的参数
            queryParams : function(params) {
                return splitFormSerializeValue('queryForm',params);
            },
            url : '${basePath}/manage/serialnum/list'
        });
    })
    //重置按钮事件
    $("#reset_btn").click(function(){
        $("#queryForm").resetForm();
    });

    $('#begintime,#endtime').datepicker();
    var startTime = new Date();
    startTime.setDate(startTime.getDate()+1);
    var endTime = startTime;

    $('#begintime').val(getPreMonth(getSmpFormatDate(startTime,false)));
    $('#endtime').val(getSmpFormatDate(endTime,false));

    var $table = $('#table');
    $(function() {
        // bootstrap table初始化
        $table.bootstrapTable({
            url: '${basePath}/manage/serialnum/list',
            height: getNoHeadHeight($('#queryHead').height),
            detailFormatter: 'detailFormatter',
            paginationLoop: false,
            silentSort: false,
            smartDisplay: false,
            escape: true,
            searchOnEnterKey: true,
            maintainSelected: true,
            toolbar: '#toolbar',               //工具按钮用哪个容器
            striped: true,                      //是否显示行间隔色
            cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
            pagination: true,                   //是否显示分页（*）
            sortable: true,                     //是否启用排序
            sortOrder: "asc",                   //排序方式
            sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
            pageNumber: 1,                      //初始化加载第一页，默认第一页,并记录
            pageSize: 10,                     //每页的记录行数（*）
            pageList: [10, 25, 50, 100,500],        //可供选择的每页的行数（*）
            search: false,                      //是否显示表格搜索
            strictSearch: true,
            showColumns: true,                  //是否显示所有的列（选择显示的列）
            showRefresh: true,                  //是否显示刷新按钮
            minimumCountColumns: 2,             //最少允许的列数
            clickToSelect: true,                //是否启用点击选中行
            showToggle: true,                   //是否显示详细视图和列表视图的切换按钮
            cardView: false,                    //是否显示详细视图
            detailView: false,                  //是否显示父子表
            idField: 'id',
            //得到查询的参数
            queryParams : function(params) {
                return splitFormSerializeValue('queryForm',params);
            },
            columns: [
                {field: 'ck', checkbox: true},
                {field: 'machineCode', title: '完整出厂编号'},
                {field: 'years', title: '年代'},
                {field: 'classcode', title: '产品分类',formatter: function (value, row, index) {
                        return getDictText("UM_UMBRELLA_CLASSCODE",value);
                    }},
                {field: 'version', title: '型号代号',formatter: function (value, row, index) {
                        return getDictText("UM_MACHINE_TYPE",value);
                    }},
                {field: 'subversion', title: '升级或变型代号',formatter: function (value, row, index) {
                        return getDictText("UM_UMBRELLA_MSUBVERSION",value);
                    }},
                {field: 'months', title: '月份'},
                {field: 'batchnumber', title: '批次'},
                {field: 'serialnumber', title: '序号'},
                {field: 'status', title: '是否同步自有平台',formatter: function (value, row, index) {
                        return getDictText("UPMS_YES_NO",value);
                    }},
                {field: 'direction', title: '使用去向',formatter: function (value, row, index) {
                        return getDictText("UM_UMBRELLA_MDIRECTION",value);
                    }},
                {field: 'agentid', title: '代理'},
                {field: 'pathcount', title: '分机数量'},
                {field: 'directionremark', title: '使用去向备注'},
                {field: 'warrantystarttime', title: '保修开始时间',formatter:'detailLongDateTimeFormatter'},
                {field: 'warrantyendtime', title: '保修结束时间',formatter:'detailLongDateTimeFormatter'},
                //{field: 'createuser', title: '记录人'},
                //{field: 'createtime', title: '记录时间',formatter:'detailLongDateTimeFormatter'},
                {field: 'updateuser', title: '最后修改人'},
                {field: 'updatetime', title: '最后修改时间',formatter:'detailLongDateTimeFormatter'},
                {field: 'action', title: '操作', align: 'center', formatter: 'actionFormatter', events: 'actionEvents', clickToSelect: true}
            ]
            <shiro:hasPermission name="umbrella:serialnum:export">
            ,
            //导出excel表格设置
            showExport: phoneOrPc(),              //是否显示导出按钮(此方法是自己写的目的是判断终端是电脑还是手机,电脑则返回true,手机返回falsee,手机不显示按钮)
            exportDataType: "basic",              //basic', 'all', 'selected'.
            exportTypes:['excel','xlsx'],	    //导出类型
            exportOptions:{
                //ignoreColumn: [0,0],            //忽略某一列的索引
                fileName: '出厂编号数据导出',              //文件名称设置
                excelstyles: ['background-color', 'color', 'font-size', 'font-weight'],
                //onMsoNumberFormat: DoOnMsoNumberFormat
            }
           //导出excel表格设置
            </shiro:hasPermission>
        });
    });
    window.actionEvents = {
        <shiro:hasPermission name="umbrella:serialnum:deliver">
        'click .deliver': function(e, value, row, index) {
            deliverAction([row]);
        }
        </shiro:hasPermission>
    };

    // 格式化操作按钮
    function actionFormatter(value, row, index) {
        return [
            <shiro:hasPermission name="umbrella:serialnum:deliver">
            '<a class="deliver" href="javascript:;" data-toggle="tooltip" title="出货更新"><i class="glyphicon glyphicon-edit"></i></a>　',
            </shiro:hasPermission>
        ].join('');
    }
    // 批量新增
    var createDialog;
    function createAction(rows) {
        createDialog = openDialog('url:${basePath}/manage/serialnum/create','批量新增(按规则)')
    }
    // 批量新增
    function createCustomAction(rows) {
        createDialog = openDialog('url:${basePath}/manage/serialnum/createcustom','批量新增(自定义)')
    }
    //批量出货更新
    var deliverDialog;
    function deliverAction(rows){
        if(!rows)
            rows = $table.bootstrapTable('getSelections');
        if (rows.length == 0) {
            infoDialog( '请至少选择一条记录！');
        } else {
            var ids = new Array();
            for (var i in rows) {
                ids.push(rows[i].id);
            }
           deliverDialog = openDialog('url:${basePath}/manage/serialnum/deliver/'+ids.join("-"),'出货更新');
        }
    }

    // 同步到自有平台
    var synownplatDialog;
    function synownplatformAction(rows) {
        if(!rows)
            rows = $table.bootstrapTable('getSelections');
        if (rows.length == 0) {
            infoDialog( '请至少选择一条记录！');
        } else {
            synownplatDialog = confirmDialog('确认同步到自有平台吗？',function(){
                var ids = new Array();
                for (var i in rows) {
                    ids.push(rows[i].id);
                }
                $.ajax({dataType: 'json',
                    type: 'get',
                    url: '${basePath}/manage/serialnum/synownplatform/' + ids.join(","),
                    success: function(result) {
                        if (result.code != 1) {
                            if (result.data instanceof Array) {
                                $.each(result.data, function(index, value) {
                                    infoDialog(value.errorMsg);
                                });
                            } else {
                                infoDialog(result.data);
                            }
                        } else {
                            infoDialog("成功同步【" + result.data + "】台。");
                            $table.bootstrapTable('refresh');
                        }
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        infoDialog(textStatus);
                    }
                });
            });
        }
    }
    function getPreMonth(date) {
        var arr = date.split('-');
        var year = arr[0]; //获取当前日期的年份
        var month = arr[1]; //获取当前日期的月份
        var day = arr[2]; //获取当前日期的日
        var days = new Date(year, month, 0);
        days = days.getDate(); //获取当前日期中月的天数
        var year2 = year;
        var month2 = parseInt(month) - 1;
        if (month2 == 0) {
            year2 = parseInt(year2) - 1;
            month2 = 12;
        }
        var day2 = day;
        var days2 = new Date(year2, month2, 0);
        days2 = days2.getDate();
        if (day2 > days2) {
            day2 = days2;
        }
        if (month2 < 10) {
            month2 = '0' + month2;
        }
        var t2 = year2 + '-' + month2 + '-' + day2;
        return t2;
    }
</script>
</body>
</html>